package com.goldsign.pay.provider.msg.rabbit.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;

/**一个交换机绑定多个消息队列，交换机根据routing_key将消息发到对应的多个队列
 * 生产者将消息不是直接发送到队列，而是发送到X交换机，
 * 然后由交换机发送给两个队列，两个消费者各自监听一个队列，来消费消息。
 * 这种方式实现同一个消息被多个消费者消费。工作模式是同一个消息只能有一个消费者
 * @author johnny
 *
 */
/**Topic模式= 多对一,多个生产者一个消费者*/

@Configuration
public class RabbitTopicConfig {
	final static String messageA = "dev.queue.order";
	final static String messageB = "dev.queue.store";
    String topicExchange = "dev.topicExchange.shop";

    @Bean
    public Queue messageOrder() {
        return new Queue(messageA,true,false,true);
    }
    @Bean
    public Queue messageStore() {
        return new Queue(messageB,true,false,true);
    }
    
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(topicExchange);
    }
    
    @Bean
    Binding bindingExchangeOrder(Queue messageOrder, TopicExchange exchange) {
        return BindingBuilder.bind(messageOrder).to(exchange).with("#.order.#");
    }

    @Bean
    Binding bindingExchangeStore(Queue messageStore, TopicExchange exchange) {
        return BindingBuilder.bind(messageStore).to(exchange).with("#.store.#");
    }
}
